package com.gjy.nwpufindseats;

import com.gjy.nwpufindseats.Fragment.Fragment_gradelist;
import com.gjy.nwpufindseats.Fragment.Fragment_login;
import com.gjy.nwpufindseats.Fragment.Fragment_login_info;
import com.gjy.nwpufindseats.Fragment.Fragment_more;
import com.gjy.nwpufindseats.Fragment.Fragment_offdown;
import com.gjy.nwpufindseats.Fragment.Fragment_roomlist;
import com.gjy.nwpufindseats.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class LeftSlidingMenuFragment extends Fragment implements OnClickListener{
	
	private View roomBtnLayout;
	private View gradeBtnLayout;
	private View moreBtnLayout;
	private View loginBtnLayout;
	private View exitBtnLayout;
	private View ofdBtnLayout;
	SharedPreferences preferences;
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	 View view = inflater.inflate(R.layout.main_left_fragment, container,
 				false);
    	 loginBtnLayout = view.findViewById(R.id.loginBtnLayout);
   	  	 loginBtnLayout.setOnClickListener(this);
    	  roomBtnLayout = view.findViewById(R.id.roomBtnLayout);
    	  roomBtnLayout.setOnClickListener(this);
    	  gradeBtnLayout = view.findViewById(R.id.gradeBtnLayout);
    	  gradeBtnLayout.setOnClickListener(this);
    	  moreBtnLayout = view.findViewById(R.id.moreBtnLayout);
    	  moreBtnLayout.setOnClickListener(this);
    	  ofdBtnLayout = view.findViewById(R.id.ofdBtnLayout);
    	  ofdBtnLayout.setOnClickListener(this);    	  
    	  exitBtnLayout = view.findViewById(R.id.exitBtnLayout);
    	  exitBtnLayout.setOnClickListener(this);    	   		
    	return view;
    }

	@Override
	public void onClick(View v) {
		Fragment newContent = null;
		switch (v.getId()) {
		case R.id.loginBtnLayout:				
			roomBtnLayout.setSelected(false);
			gradeBtnLayout.setSelected(false);
			moreBtnLayout.setSelected(false);
			loginBtnLayout.setSelected(true);
			ofdBtnLayout.setSelected(false);
			exitBtnLayout.setSelected(false);
			preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
	    	String loginInfoString = preferences.getString("xh", "0");//未登录，则返回0，登录返回学号
	    	if("0".equals(loginInfoString))
	    	{
	    		newContent = new Fragment_login();
	    	}
	    	else 
	    	{
	    		newContent = new Fragment_login_info();
			}
		    break; 
			case R.id.roomBtnLayout:
				newContent = new Fragment_roomlist();
				roomBtnLayout.setSelected(true);
				gradeBtnLayout.setSelected(false);
				moreBtnLayout.setSelected(false);
				loginBtnLayout.setSelected(false);
				ofdBtnLayout.setSelected(false);
				exitBtnLayout.setSelected(false);
			    break;
			case R.id.gradeBtnLayout:
				newContent = new Fragment_gradelist();
				roomBtnLayout.setSelected(false);
				gradeBtnLayout.setSelected(true);
				moreBtnLayout.setSelected(false);
				loginBtnLayout.setSelected(false);
				ofdBtnLayout.setSelected(false);
				exitBtnLayout.setSelected(false);
			    break;
			case R.id.moreBtnLayout:
				newContent = new Fragment_more();
				roomBtnLayout.setSelected(false);
				gradeBtnLayout.setSelected(false);
				moreBtnLayout.setSelected(true);
				loginBtnLayout.setSelected(false);
				ofdBtnLayout.setSelected(false);
				exitBtnLayout.setSelected(false);
			    break;
			case R.id.ofdBtnLayout:
				newContent = new Fragment_offdown();
				roomBtnLayout.setSelected(false);
				gradeBtnLayout.setSelected(false);
				moreBtnLayout.setSelected(false);
				loginBtnLayout.setSelected(false);
				ofdBtnLayout.setSelected(true);
				exitBtnLayout.setSelected(false);
			    break;
			case R.id.exitBtnLayout:
				dialog();
			    break;    
			default:
				break;
		}
		
		if (newContent != null)
			switchFragment(newContent);
		
	}
	
	public void dialog() {
		  AlertDialog.Builder builder = new Builder(getActivity());
		  
		  builder.setTitle("提示");
		  builder.setMessage("确认退出软件吗？");
		  //不同的监听器
		  builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
			   @Override
			   public void onClick(DialogInterface dialog, int which) {
			    dialog.dismiss();
			    ((MainActivity) getActivity()).exit();			 
			   }

		   });
		  builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
		   @Override
		   public void onClick(DialogInterface dialog, int which) {
		    dialog.dismiss();
		   }
		  });
		  builder.create().show();
		}
	

	
	/*
	 * 切换到不同的功能内容
	 */
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;	
			MainActivity ra = (MainActivity) getActivity();
			ra.switchContent(fragment);
		
	}
}
